Exam room [Heap, Sorted Positions]¶
Time: seat:O(LogN), leave:O(LogN); Space: O(N); medium
In an exam room, there are N seats in a single row, numbered 0, 1, 2, …, N-1.
When a student enters the room, they must sit in the seat that maximizes the distance to the closest person.
If there are multiple such seats, they sit in the seat with the lowest number. (Also, if no one is in the room, then the student sits at seat number 0.)
Return a class ExamRoom(int N) that exposes two functions: ExamRoom.seat() returning an int representing what seat the student sat in, and ExamRoom.leave(int p) representing that the student in seat number p now leaves the room.
It is guaranteed that any calls to ExamRoom.leave(p) have a student sitting in seat p.
Example 1:
Input/Output:
e = ExamRoom(10)
e.seat() # 0
e.seat() # 9
e.seat() # 4
e.seat() # 2
e.leave(4)
e.seat() # 5
Explanation:
ExamRoom(10) -> null
seat() -> 0, no one is in the room, then the student sits at seat number 0.
seat() -> 9, the student sits at the last seat number 9.
seat() -> 4, the student sits at the last seat number 4.
seat() -> 2, the student sits at the last seat number 2.
leave(4) -> null
seat() -> 5, the student sits at the last seat number 5.
Notes:
1 <= N <= 10^9
ExamRoom.seat() and ExamRoom.leave() will be called at most 10^4 times across all test cases.
Calls to ExamRoom.leave(p) are guaranteed to have a student currently sitting in seat number p.
1. Heap¶
[3]:
import heapq
class ExamRoom1(object):
"""
Time: seat:O(LogN)
leave:O(LogN)
Space: O(N)
"""
def __init__(self, N):
"""
:type N: int
"""
self.__num = N
self.__seats = {-1: [-1, self.__num], self.__num: [-1, self.__num]}
self.__max_heap = [(-self.__distance((-1, self.__num)), -1, self.__num)]
def seat(self):
"""
:rtype: int
"""
while self.__max_heap[0][1] not in self.__seats or \
self.__max_heap[0][2] not in self.__seats or \
self.__seats[self.__max_heap[0][1]][1] != self.__max_heap[0][2] or \
self.__seats[self.__max_heap[0][2]][0] != self.__max_heap[0][1]:
heapq.heappop(self.__max_heap) # lazy deletion
_, left, right = heapq.heappop(self.__max_heap)
mid = 0 if left == -1 \
else self.__num-1 if right == self.__num \
else (left+right) // 2
self.__seats[mid] = [left, right]
heapq.heappush(self.__max_heap, (-self.__distance((left, mid)), left, mid))
heapq.heappush(self.__max_heap, (-self.__distance((mid, right)), mid, right))
self.__seats[left][1] = mid
self.__seats[right][0] = mid
return mid
def leave(self, p):
"""
:type p: int
:rtype: void
"""
left, right = self.__seats[p]
self.__seats.pop(p)
self.__seats[left][1] = right
self.__seats[right][0] = left
heapq.heappush(self.__max_heap, (-self.__distance((left, right)), left, right))
def __distance(self, segment):
return segment[1]-segment[0]-1 if segment[0] == -1 or segment[1] == self.__num \
else (segment[1]-segment[0]) // 2
[4]:
e = ExamRoom1(10)
assert e.seat() == 0
assert e.seat() == 9
assert e.seat() == 4
assert e.seat() == 2
e.leave(4)
assert e.seat() == 5
2. Maintain Sorted Positions [O(P), O(P)]¶
Intuition
We’ll maintain ExamRoom.students, a sorted list (or TreeSet in Java) of positions the students are currently seated in.
Algorithm
The ExamRoom.leave(p) operation is clear - we will just list.remove (or TreeSet.remove) the student from ExamRoom.students.
Let’s focus on the ExamRoom.seat() : int operation. For each pair of adjacent students i and j, the maximum distance to the closest student is d = (j - i) / 2, achieved in the left-most seat i + d. Otherwise, we could also sit in the left-most seat, or the right-most seat.
Finally, we should handle the case when there are no students separately.
For more details, please review the comments made in the implementations.
[9]:
import bisect
class ExamRoom2(object):
"""
Time: Each seat operation is O(P), where P is the number of students sitting,
as we iterate through every student.
Each leave operation is O(P)(logP in Java).
Space: O(P), the space used to store the positions of each student sitting.
"""
def __init__(self, N):
self.N = N
self.students = []
def seat(self):
# Let's determine student, the position of the next
# student to sit down.
if not self.students:
student = 0
else:
# Tenatively, dist is the distance to the closest student,
# which is achieved by sitting in the position 'student'.
# We start by considering the left-most seat.
dist, student = self.students[0], 0
for i, s in enumerate(self.students):
if i:
prev = self.students[i-1]
# For each pair of adjacent students in positions (prev, s),
# d is the distance to the closest student;
# achieved at position prev + d.
d = (s - prev) // 2
if d > dist:
dist, student = d, prev + d
# Considering the right-most seat.
d = self.N - 1 - self.students[-1]
if d > dist:
student = self.N - 1
# Add the student to our sorted list of positions.
bisect.insort(self.students, student)
return student
def leave(self, p):
self.students.remove(p)
[10]:
e = ExamRoom2(10)
assert e.seat() == 0
assert e.seat() == 9
assert e.seat() == 4
assert e.seat() == 2
e.leave(4)
assert e.seat() == 5
See also:¶
Related problems:¶
https://leetcode.com/problems/maximize-distance-to-closest-person/